/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.swt.accessibility.AccessibleEvent;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * 
 */
abstract public class ContainerEditPart extends GraphNodeEditPart {
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected AccessibleEditPart createAccessible() {
		return new AccessibleGraphicalEditPart() {
			@Override
			public void getName(AccessibleEvent e) {
				e.result = getGraphNode().toString();
			}
		};
	}

	/**
	 * A container is GraphNode that has contained elements (ex. diagram,
	 * package...)
	 * 
	 * @param modelManager 
	 * @param graphNode 
	 */
	public ContainerEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * Installs the desired EditPolicies for this.
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		// this is a container so...
		installEditPolicy(
				EditPolicy.CONTAINER_ROLE
				, new ContainerWithSemanticEditPolicy());
		installEditPolicy(
			DimensionAdaptationEditPolicy.DIMENSION_ADAPTATION
				, new ContainerDimensionAdaptationEditPolicy());
	}

	/**
	 * Returns the children of this through the model.
	 * 
	 * @return Children of this as a List.
	 */
	@Override
	protected List getModelChildren() {
		ArrayList contained= new ArrayList();
		Iterator it=getGraphNode().getContained().iterator();
		while (it.hasNext()){
			GraphElement ge=(GraphElement)it.next();
			if (!(ge instanceof GraphEdge)){
				contained.add(ge);
			}
		}

		return contained;

	}

	/**
	 * 
	 * 
	 * @param evt 
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		super.propertyChange(evt);
		String prop = evt.getPropertyName();
		Debug.debug(this,"Container EditPart Evenment-----> "+prop,channel);
		refreshVisuals();
	}

	/**
	 * 
	 * 
	 * @param notification 
	 */
	@Override
	public void notifyChanged(Notification notification) {
		int type = notification.getEventType();
		//it allows debuging
		Debug.debug(this,"Container EditPart Notification ----->:"+ type,channel);
		Debug.debug(this,"   ADD: "+ Notification.ADD
				+" ADD_MANY: "+ Notification.ADD_MANY
				+" EVENT_TYPE_COUNT: "+	Notification.EVENT_TYPE_COUNT
				+" MOVE: "+Notification.MOVE
				+" REMOVE: "+	Notification.REMOVE
				+" REMOVE_MANY: "+Notification.REMOVE_MANY
				+" SET: "+Notification.SET,channel);
		if((type==Notification.SET)||(type==Notification.ADD)){
			modelManager.modifGraphElement(this.getGraphNode());
		}
		refresh();
		
	}
}
